package com.wowangz.cms.syscore.autotask.job;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.reflect.TypeToken;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.services.pojo.Menu;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.autotask.service.MenuSyncService;
import com.wowangz.cms.syscore.utils.JedisUtil;
import com.wowangz.cms.systemmanage.currency.api.CurrencyController;
import com.wowangz.cms.systemmanage.menuauthority.pojo.MenuAuthority;

public class MenuSync extends BaseJob{

	private static Logger logger = LoggerFactory.getLogger(CurrencyController.class);
	
	/**
	 * 同步所有菜单信息至redis
	 */
	@Override
	public void innerExecute(JobExecutionContext context) throws Exception
	{
		logger.info("进入同步菜单自动任务");
		try
		{
			//查询所有菜单信息同步至redis
			Menu queryMenu = new Menu();
			queryMenu.setStatusid(SysConstant.RecordStatus.VALID);
			MenuSyncService menuSynchronizationService = (MenuSyncService) IssTransactionContainer.getInstance(MenuSyncService.class, false);
			List<Menu> list = menuSynchronizationService.queryAutotaskList(queryMenu);
			Map map = new HashMap();
			for (Menu menu : list)
			{
				map.put(menu.getId(), menu);
			}
			JedisUtil.insertPublicDataObject(JedisUtil.ALL_MENU_KEY, map,  JedisUtil.MENU);
			
			Type type = new TypeToken<HashMap<Long,Menu>>() {}.getType();
			HashMap hashmap = (HashMap)JedisUtil.getPublicDataObjectByType(JedisUtil.ALL_MENU_KEY, type, JedisUtil.MENU);
			
			//查询系统管理菜单信息同步至redis
			queryMenu.setMenutype(SysConstant.MenuType.ADMIN);;
			List<Menu> systemMenuList = menuSynchronizationService.querySystemMenuList(queryMenu);
			JedisUtil.insertPublicDataObject(JedisUtil.SYSTE_MENU_KEY, systemMenuList, JedisUtil.MENU);
			System.out.println("系统管理菜单列表" +   JedisUtil.getPublicDataObject(JedisUtil.SYSTE_MENU_KEY, List.class, JedisUtil.MENU));
			
			queryMenu.setMenutype(SysConstant.MenuType.NORNAL);;
			List<Menu> storeMenuList = menuSynchronizationService.queryStoreMenuList(queryMenu);
			JedisUtil.insertPublicDataObject(JedisUtil.STORE_MENU_KEY, storeMenuList, JedisUtil.MENU);
			System.out.println("业务菜单列表" +   JedisUtil.getPublicDataObject(JedisUtil.STORE_MENU_KEY, List.class, JedisUtil.MENU));
			
			
			//同步菜单权限
			List<MenuAuthority> menuAuthority = menuSynchronizationService.queryMenuAuthorityList();
			Map<String,MenuAuthority> authorityMap = new HashMap<String, MenuAuthority>();
			for (MenuAuthority menuAuthority2 : menuAuthority)
			{
				authorityMap.put(menuAuthority2.getUrl(), menuAuthority2);
			}
			JedisUtil.insertPublicDataObject(JedisUtil.MENU_AUTHORITY, authorityMap, JedisUtil.MENU);
			Type typeList = new TypeToken<HashMap<String,MenuAuthority>>() {}.getType();
			Map<String,MenuAuthority> tmp = (HashMap<String,MenuAuthority>)JedisUtil.getPublicDataObjectByType(JedisUtil.MENU_AUTHORITY, typeList, JedisUtil.MENU);
			System.out.println("菜单权限表：" +  tmp.keySet());
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		logger.info("同步菜单自动任务运行结束");
	}

}
